﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using System.Text;
using System.Xml;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;

namespace Content_Extension
{
    [ContentTypeWriter]
    public class TileMapWriter : ContentTypeWriter<TileMapContent>
    {
        protected override void Write(ContentWriter output, TileMapContent value)
        {
            output.Write(value.Layers.Count);
            output.Write(value.Objects.Count);

            output.Write(value.Textures.Count);

            foreach (TileSetTextureContent textureContent in value.Textures)
            {
                output.WriteExternalReference<TextureContent>(textureContent.Texture);
                output.Write(textureContent.tileWidth);
                output.Write(textureContent.tileHeight);
            }

            foreach (TileLayerContent layer in value.Layers)
            {
                output.Write(layer.Layout.GetLength(0));
                output.Write(layer.Layout.GetLength(1));
                for (int y = 0; y < layer.Layout.GetLength(0); y++)
                    for (int x = 0; x < layer.Layout.GetLength(1); x++)
                        output.Write(layer.Layout[y, x]);

                output.Write(layer.name);
            }

            foreach (ObjectLayerContent objs in value.Objects)
            {
                output.Write(objs.gridId);
                output.Write(objs.x);
                output.Write(objs.y);
                output.Write(objs.width);
                output.Write(objs.height);
            }

        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {

            return "Client.TileLevelReader, Client";
        }
    }
}
